package cn.paranoid.realm;

import cn.paranoid.service.SecurityService;
import cn.paranoid.service.impl.SecurityServiceImpl;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import javax.annotation.Resource;

public class DefinitionRealm extends AuthorizingRealm {
    /**
     * 认证方法
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //获取登录名
        String loginName = (String) authenticationToken.getPrincipal();
        SecurityService service = new SecurityServiceImpl();
        String password = service.findPasswordByLoginName(loginName);
        if ("".equals(password)||password == null){
            throw new UnknownAccountException("帐号不存在");
        }
        return new SimpleAuthenticationInfo(loginName,password,getName()); //realm有多个，这里必须传过去名字来确定是哪个realm
    }

    /**
     *鉴权方法
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }


}
